program queens;
	const
		chessboard_size = eight;
		zero = 0;
		one = zero + 1;
		eight = 8 * one;
	type
		chessboard = record
				num_queens : integer;
				used_cols : array [one..chessboard_size] of integer;
				free_lins : array [one..chessboard_size] of boolean
			end;
	procedure solver();
		var
			num_solutions : integer;
			empty_chessboard : chessboard;
			i : integer;
		procedure solve(current_chessboard : ^chessboard);
			var
				i : integer;
				j : integer;
				ok : boolean;		
		begin
			if current_chessboard^.num_queens = chessboard_size then
				begin
					for j := one to chessboard_size do
						for i := one to chessboard_size do
							if current_chessboard^.used_cols[i] = j
								then i := i  { write('Q'); }
								else i := i; { write(' '); }
					num_solutions := num_solutions + 1
				end
			else
				begin
					for j := one to chessboard_size do
						if current_chessboard^.free_lins[j] then
							begin
								ok := true;
								for i := one to current_chessboard^.num_queens do
									if (current_chessboard^.num_queens + 1) - i = (current_chessboard^.used_cols[i] - j) or
									   (current_chessboard^.num_queens + 1) - i = (j - current_chessboard^.used_cols[i]) then
										ok := false;
								if ok then
									begin
										current_chessboard^.num_queens := current_chessboard^.num_queens + 1;
										current_chessboard^.used_cols[current_chessboard^.num_queens] := j;
										current_chessboard^.free_lins[j] := false;
										solve(current_chessboard);
										current_chessboard^.free_lins[j] := true;
										current_chessboard^.used_cols[current_chessboard^.num_queens] := 0;
										current_chessboard^.num_queens := current_chessboard^.num_queens - 1
									end
							end
				end
		end;
	begin
		for i := one to chessboard_size do
			begin
				empty_chessboard.used_cols[i] := 0;
				empty_chessboard.free_lins[i] := true
			end;
		empty_chessboard.num_queens := 0;
		solve(^empty_chessboard)
	end;
begin
	solver()
end.
